package com.example.demo.service.order.impl;

import com.alibaba.fastjson.JSON;
import com.example.demo.common.BaseErrorConfigEnum;
import com.example.demo.common.BasePageResponse;
import com.example.demo.common.ResultObject;
import com.example.demo.common.TerminologyConfigEnum;
import com.example.demo.dao.IAddressDao;
import com.example.demo.jooq.tables.daos.AddressDao;
import com.example.demo.jooq.tables.pojos.Address;
import com.example.demo.model.po.AddressPage;
import com.example.demo.model.request.AddressPageRequest;
import com.example.demo.model.request.AddressRequest;
import com.example.demo.model.request.VpsPageRequest;
import com.example.demo.model.response.AddressResponse;
import com.example.demo.service.BaseBusinessServiceImpl;
import com.example.demo.service.order.IAddressService;
import com.example.demo.util.AssertUtils;
import com.example.demo.util.ClazzUtils;
import com.example.demo.util.DateUtils;
import org.modelmapper.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class IAddressServiceImpl extends BaseBusinessServiceImpl implements IAddressService {
    @Autowired
    private IAddressDao iAddressDao;

    @Autowired
    private AddressDao addressDao;
    @Override
    public ResultObject<BasePageResponse<AddressResponse>> queryPageAddressList(AddressPageRequest addressPageRequest) {
        ResultObject<BasePageResponse<AddressResponse>> responseResultObject = new ResultObject<>();
        List<AddressPage> vpsPages =  iAddressDao.queryPageAddressList(addressPageRequest);
        if(AssertUtils.isNotEmpty(vpsPages)){
            Integer integer =vpsPages.get(0).getTotalLine();
            List<AddressResponse> vpsResponses = (List<AddressResponse>)this.converterList(vpsPages,new TypeToken<List<AddressResponse>>() {}.getType() );
            //术语转换
            vpsResponses.forEach(visaResponse -> {
                if(AssertUtils.isNotNull(visaResponse.getAddressStatus())){
                    Arrays.stream(TerminologyConfigEnum.ADDRESS_STATUS.values()).filter(value->value.name().equals(visaResponse.getAddressStatus())).forEach(value->visaResponse.setAddressStatus(value.desc()));
                }
                if(AssertUtils.isNotNull(visaResponse.getSite())){
                    Arrays.stream(TerminologyConfigEnum.SITE.values()).filter(value->value.name().equals(visaResponse.getSite())).forEach(value->visaResponse.setSite(value.desc()));
                }
            });
            BasePageResponse<AddressResponse> responseBasePageResponse =BasePageResponse.getData(addressPageRequest.getCurrentPage(),addressPageRequest.getPageSize(),integer,vpsResponses);
            responseResultObject.setData(responseBasePageResponse);
        }
        return responseResultObject;
    }

    @Override
    public ResultObject saveAddress(AddressRequest addressRequest) {
        this.getLogger().info("保存地址,请求数据为:{}", JSON.toJSONString(addressRequest));
        ResultObject resultObject = new ResultObject();
        //参数校验
        vaildAddressParameter(addressRequest);
        Address address = null;
        if(AssertUtils.isNotNull(addressRequest.getAddressId())){
            address=addressDao.fetchOneByAddressId(addressRequest.getAddressId());
            AssertUtils.isNotNull(this.getLogger(),address, BaseErrorConfigEnum.PASSWORD_IS_NOT_NULL);
        }else {
            address = new Address();
        }
        ClazzUtils.copyPropertiesIgnoreNull(addressRequest,address);
        //持久化
        if(AssertUtils.isNotEmpty(addressRequest.getAddressId())){
            address.setUpdatedDate(System.currentTimeMillis());
            addressDao.update(address);
        }else {
            address.setAddressStatus(AssertUtils.isNotNull(addressRequest.getAddressStatus())?addressRequest.getAddressStatus():TerminologyConfigEnum.EMAIL_STATUS.NEW.name());
            address.setAddressId(DateUtils.getJobNumberByTime2("ADDR",null,DateUtils.FORMATE53,false));
            address.setCreatedDate(System.currentTimeMillis());
            address.setUpdatedDate(System.currentTimeMillis());
            addressDao.insert(address);
        }
        return resultObject;
    }

    private void vaildAddressParameter(AddressRequest addressRequest) {
        AssertUtils.isNotNull(this.getLogger(),addressRequest.getSite(), BaseErrorConfigEnum.SITE_IS_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),addressRequest.getName(), BaseErrorConfigEnum.ADDRESS_NAME_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),addressRequest.getPhone(), BaseErrorConfigEnum.PHONE_IS_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),addressRequest.getTaxFlag(), BaseErrorConfigEnum.TAX_CITY_IS_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),addressRequest.getCountry(), BaseErrorConfigEnum.COUNTRY_IS_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),addressRequest.getCity(), BaseErrorConfigEnum.CITY_IS_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),addressRequest.getAddress(), BaseErrorConfigEnum.ADDRESS_DETAIL_IS_NOT_NULL);
    }

    @Override
    public ResultObject<AddressResponse> queryAddressDetail(String addressId) {
        ResultObject<AddressResponse> resultObject = new ResultObject<>();
        Address address = addressDao.fetchOneByAddressId(addressId);
        if(AssertUtils.isNotNull(address)){
            AddressResponse addressResponse =(AddressResponse)this.converterObject(address,AddressResponse.class);
            resultObject.setData(addressResponse);
        }
        return resultObject;
    }
}
